package com.qingxin.web.action;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.park.entity.BatchProductsView;
import com.qingxin.park.service.BatchProductsService;
import com.qingxin.park.vo.TraceData;
import com.qingxin.repertory.entity.Repertory;
import com.qingxin.repertory.service.RepertoryService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.web.service.TraceQueryService;

/**
 * 追溯查询
 * @author Administrator
 */
@Controller
public class TraceQueryAction extends BaseAction {

	@Autowired
	private TraceQueryService traceQueryService;
	@Autowired
	private BatchProductsService batchProductsService;
	@Autowired
	private RepertoryService repertoryService;
	
	
	/**
	 * 手机端二维码查询页面
	 * @param model
	 * @param code
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/mq/traceQuery.do", method=RequestMethod.GET)
	public String mobileTraceQuery(ModelMap model, String code, HttpServletRequest request){
		model.put("model", "index");
		if(StringUtils.isNotEmpty(code) && traceQueryService.verifyCode(code)){
			char type = code.charAt(0);
			switch (type) {
			case '1':
				queryZZList(model, code, request);
				return "mobile/traceQueryList";
			case '2':
				queryYZList(model, code, request);
				return "mobile/traceQueryList";
			case '3':
				queryJGList(model, code, request);
				return "mobile/traceQueryList";
			case '5':
				queryCompany(model, code, request);
				return "mobile/companyQuery";
			}
		}
		return "mobile/traceQuery";
	}
	
	
	//种植信息
	private String queryZZList(ModelMap model, String code, HttpServletRequest request){
		BatchProductsView products = batchProductsService.getProductsViewByPbn(code);
		model.put("code", code);
		model.put("products", products);
		
		TraceData traceData = traceQueryService.getTraceData(code, false);
		model.put("traceData", traceData);
		
		return "cms/traceQuery";
	}
	
	//养殖信息
	private String queryYZList(ModelMap model, String code, HttpServletRequest request){
		BatchProductsView products = batchProductsService.getProductsViewByPbn(code);
		model.put("code", code);
		model.put("products", products);
		
		TraceData traceData = traceQueryService.getTraceData(code, false);
		model.put("traceData", traceData);
		
		return "cms/traceQuery";
	}
	
	//加工信息
	private String queryJGList(ModelMap model, String code, HttpServletRequest request){
		BatchProductsView products = batchProductsService.getProductsViewByPbn(code);
		model.put("code", code);
		model.put("products", products);
		
		TraceData traceData = traceQueryService.getTraceData(code, false);
		model.put("traceData", traceData);
		
		return "cms/traceQuery";
	}
	
	
	
	/**
	 * 二维码查询页面
	 * @param model
	 * @param code
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/c/traceQuery.do", method=RequestMethod.GET)
	public String traceQuery(ModelMap model, String code, HttpServletRequest request){
		model.put("model", "index");
		if(StringUtils.isNotEmpty(code) && traceQueryService.verifyCode(code)){
			char type = code.charAt(0);
			switch (type) {
			case '1':
				return queryZZ(model, code, request);
			case '2':
				return queryYZ(model, code, request);
			case '3':
				return queryJG(model, code, request);
			case '5':
				return queryCompany(model, code, request);
			}
		}
		return "cms/traceQuery";
	}
	
	//种植信息
	private String queryZZ(ModelMap model, String code, HttpServletRequest request){
		BatchProductsView products = batchProductsService.getProductsViewByPbn(code);
		model.put("code", code);
		model.put("products", products);
		
		TraceData traceData = traceQueryService.getTraceData(code, true);
		model.put("traceData", traceData);
		
		return "cms/traceQuery";
	}
	
	//养殖信息
	private String queryYZ(ModelMap model, String code, HttpServletRequest request){
		BatchProductsView products = batchProductsService.getProductsViewByPbn(code);
		model.put("code", code);
		model.put("products", products);
		
		TraceData traceData = traceQueryService.getTraceData(code, true);
		model.put("traceData", traceData);
		
		return "cms/traceQuery";
	}
	
	//加工信息
	private String queryJG(ModelMap model, String code, HttpServletRequest request){
		BatchProductsView products = batchProductsService.getProductsViewByPbn(code);
		model.put("code", code);
		model.put("products", products);
		
		TraceData traceData = traceQueryService.getTraceData(code, true);
		model.put("traceData", traceData);
		
		return "cms/traceQuery";
	}
	
	//企业信息
	private String queryCompany(ModelMap model, String code, HttpServletRequest request){
		model.put("code", code);
		
		TraceData traceData = traceQueryService.getTraceData(code, true);
		model.put("traceData", traceData);
		
		return "cms/companyQuery";
	}
	
	
	/**
	 * 获取追溯数据
	 * @param code
	 * @param simple
	 * @return
	 */
	@RequestMapping(value="/c/traceQuery.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object traceQuery(String code, String simple){
		TraceData traceData = traceQueryService.getTraceData(code, !"true".equals(simple));
		return traceData;
	}
	
	
	/**
	 * 获取农资信息
	 * @param mtid
	 * @return
	 */
	@RequestMapping(value="/c/getProRecordMaterial.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object getProRecordMaterial(String mtid){
		Repertory repertory = repertoryService.get(mtid);
		return repertory;
	}
	
	
	
}
